import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grocery_app_demo/api/entity/dict.dart';
import 'package:grocery_app_demo/api/entity/goods_list_res_entity.dart';
import 'package:grocery_app_demo/api/entity/shipper_list_res_entity.dart';
import 'package:grocery_app_demo/api/entity/supplier_list_by_type_res_entity.dart';
import 'package:grocery_app_demo/api/entity/warehouse_list_res_entity.dart';
import 'package:grocery_app_demo/api/scm_api.dart';
import 'package:grocery_app_demo/core/utils/logger.dart';
import 'package:grocery_app_demo/pages/purchase_main/purchase_create/goods_info.dart';
import 'package:grocery_app_demo/routes/app_routes.dart';

class CreatePurchaseLogic extends GetxController {
  RxnInt supplierId = RxnInt();
  RxnInt warehouseId = RxnInt();
  RxnInt shipperId = RxnInt();
  RxMap<String, GoodsInfo> goodsMap = RxMap();

  RxList<SupplierListByTypeResEntity> supplierList = RxList();
  RxList<WarehouseListResEntity> warehouseList = RxList();
  RxList<ShipperListResEntity> shipperList = RxList();

  void submit() {
    Logger.debug(supplierId.value);
  }

  @override
  void onReady() {
    ScmApi().getList<SupplierListByTypeResEntity>(ScmApi.listSupplierByType,
        data: {
          'supplierType': 'GOODS'
        }).then((value) => supplierList.addAll(value));

    ScmApi()
        .getList<WarehouseListResEntity>(ScmApi.listWarehouse)
        .then((value) => warehouseList.addAll(value));

    ScmApi()
        .getList<ShipperListResEntity>(ScmApi.listShipper)
        .then((value) => shipperList.addAll(value));
  }

  List<DropdownMenuItem<int?>> buildSupplier() {
    List<DropdownMenuItem<int?>> list = [];
    list.add(const DropdownMenuItem<int?>(
      value: null,
      child: Text('请选择供应商'),
    ));
    list.addAll(supplierList
        .map((e) => DropdownMenuItem<int?>(
              value: e.id,
              child: Text(e.name),
            ))
        .toList());
    return list;
  }

  addGoods() {
    Get.toNamed(AppRoutes.selectGoods, arguments: {
      'selected': goodsMap.keys,
      'callback': (List<GoodsListResEntity> goodsList) {
        if (goodsList.isEmpty) {
          goodsMap.clear();
        } else {
          for (var goods in goodsList) {
            goodsMap.putIfAbsent(
                goods.goodsCode,
                () => GoodsInfo(
                    goodsName: goods.goodsName,
                    goodsCode: goods.goodsCode,
                    goodsUnit: Unit.nameOf(goods.unit),
                    standardWeight: goods.standardWeight,
                    goodsCodeInfoList: [],
                    price: 0.0,
                    serviceCost: 0.0,
                    totalCount: 0.0,
                    totalWeight: 0.0,
                    expand: false));
          }
          goodsMap.removeWhere((key, goodsInfo) =>
              goodsList.every((element) => element.goodsCode != key));
        }
      }
    });
  }

  List<DropdownMenuItem<int?>> buildWarehouse() {
    List<DropdownMenuItem<int?>> list = [];
    list.add(const DropdownMenuItem<int?>(
      value: null,
      child: Text('请选择仓库'),
    ));
    list.addAll(warehouseList
        .map((e) => DropdownMenuItem<int?>(
              value: e.id,
              child: Text(e.whName),
            ))
        .toList());
    return list;
  }

  List<DropdownMenuItem<int?>> buildShipper() {
    List<DropdownMenuItem<int?>> list = [];
    list.add(const DropdownMenuItem<int?>(
      value: null,
      child: Text('请选择货主'),
    ));
    list.addAll(shipperList
        .map((e) => DropdownMenuItem<int?>(
              value: e.id,
              child: Text(e.shipperName),
            ))
        .toList());
    return list;
  }
}
